package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Arrays;

import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.*;

/**
 * C -> S
 * 充电桩应答服务器查询最近一次充电各时段信息
 * 充电桩上报充电信息
 */
@Data
public class Cmd114 extends ProtocolDataBase {

    public static final int MIN_SIZE = 142;

    private int v1;
    private int v2;
    private String sn;                          // 充电桩编码
    private int pileNum;                        // 充电枪号
    private int workingStatus;                  // 工作状态
    private String startTime;                   // 开始充电开始时间
    private double t1ChargingQuantity;          // 时段1充电电量
    private double t2ChargingQuantity;
    private double t3ChargingQuantity;
    private double t4ChargingQuantity;
    private double t5ChargingQuantity;
    private double t6ChargingQuantity;
    private double t7ChargingQuantity;
    private double t8ChargingQuantity;
    private double t9ChargingQuantity;
    private double t10ChargingQuantity;
    private double t11ChargingQuantity;
    private double t12ChargingQuantity;
    private double t13ChargingQuantity;
    private double t14ChargingQuantity;
    private double t15ChargingQuantity;
    private double t16ChargingQuantity;
    private double t17ChargingQuantity;
    private double t18ChargingQuantity;
    private double t19ChargingQuantity;
    private double t20ChargingQuantity;
    private double t21ChargingQuantity;
    private double t22ChargingQuantity;
    private double t23ChargingQuantity;
    private double t24ChargingQuantity;
    private double t25ChargingQuantity;
    private double t26ChargingQuantity;
    private double t27ChargingQuantity;
    private double t28ChargingQuantity;
    private double t29ChargingQuantity;
    private double t30ChargingQuantity;
    private double t31ChargingQuantity;
    private double t32ChargingQuantity;
    private double t33ChargingQuantity;
    private double t34ChargingQuantity;
    private double t35ChargingQuantity;
    private double t36ChargingQuantity;
    private double t37ChargingQuantity;
    private double t38ChargingQuantity;
    private double t39ChargingQuantity;
    private double t40ChargingQuantity;
    private double t41ChargingQuantity;
    private double t42ChargingQuantity;
    private double t43ChargingQuantity;
    private double t44ChargingQuantity;
    private double t45ChargingQuantity;
    private double t46ChargingQuantity;
    private double t47ChargingQuantity;
    private double t48ChargingQuantity;

    public Cmd114(){
        super.setCmd(114);
    }

    public static Cmd114 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd114 cmd = new Cmd114();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 32字节充电桩编码
        cmd.sn = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 1字节充电枪号
        cmd.pileNum = getUint8(dataField[offset++]);
        // 1字节工作状态
        cmd.workingStatus = getUint8(dataField[offset++]);
        // 8字节充电开始时间
        cmd.startTime = getStandardTime(dataField, offset);
        offset += 8;
        // 2字节时段1充电电量
        cmd.t1ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段2充电电量
        cmd.t2ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段3充电电量
        cmd.t3ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段4充电电量
        cmd.t4ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段5充电电量
        cmd.t5ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段6充电电量
        cmd.t6ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段7充电电量
        cmd.t7ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段8充电电量
        cmd.t8ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段9充电电量
        cmd.t9ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段10充电电量
        cmd.t10ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段11充电电量
        cmd.t11ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段12充电电量
        cmd.t12ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段13充电电量
        cmd.t13ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段14充电电量
        cmd.t14ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段15充电电量
        cmd.t15ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段16充电电量
        cmd.t16ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段17充电电量
        cmd.t17ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段18充电电量
        cmd.t18ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段19充电电量
        cmd.t19ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段20充电电量
        cmd.t20ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段21充电电量
        cmd.t21ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段22充电电量
        cmd.t22ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段23充电电量
        cmd.t23ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段24充电电量
        cmd.t24ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段25充电电量
        cmd.t25ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段26充电电量
        cmd.t26ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段27充电电量
        cmd.t27ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段28充电电量
        cmd.t28ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段29充电电量
        cmd.t29ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段30充电电量
        cmd.t30ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段31充电电量
        cmd.t31ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段32充电电量
        cmd.t32ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段33充电电量
        cmd.t33ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段34充电电量
        cmd.t34ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段35充电电量
        cmd.t35ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段36充电电量
        cmd.t36ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段37充电电量
        cmd.t37ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段38充电电量
        cmd.t38ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段39充电电量
        cmd.t39ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段40充电电量
        cmd.t40ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段41充电电量
        cmd.t41ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段42充电电量
        cmd.t42ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段43充电电量
        cmd.t43ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段44充电电量
        cmd.t44ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段45充电电量
        cmd.t45ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段46充电电量
        cmd.t46ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段47充电电量
        cmd.t47ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 2;
        // 2字节时段48充电电量
        cmd.t48ChargingQuantity = new BigDecimal(getUint16(dataField, offset)).multiply(PERCENT).doubleValue();
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd114#").append("\n");
        buffer.append("v1:").append(v1).append("\n");
        buffer.append("v2:").append(v2).append("\n");
        buffer.append("充电桩编码:").append(sn).append("\n");
        buffer.append("充电枪号:").append(pileNum).append("\n");
        buffer.append("工作状态:").append(workingStatus).append("\n");
        buffer.append("开始充电开始时间:").append(startTime).append("\n");
        buffer.append("时段1充电电量:").append(t1ChargingQuantity).append("\n");
        buffer.append("时段2充电电量:").append(t2ChargingQuantity).append("\n");
        buffer.append("时段3充电电量:").append(t3ChargingQuantity).append("\n");
        buffer.append("时段4充电电量:").append(t4ChargingQuantity).append("\n");
        buffer.append("时段5充电电量:").append(t5ChargingQuantity).append("\n");
        buffer.append("时段6充电电量:").append(t6ChargingQuantity).append("\n");
        buffer.append("时段7充电电量:").append(t7ChargingQuantity).append("\n");
        buffer.append("时段8充电电量:").append(t8ChargingQuantity).append("\n");
        return buffer.toString();
    }
}
